#!/bin/bash
set -e

chown -R nginx:nginx /data/storage

# Enable manual start mode for newrelic-daemon to prevent it from automatically start
cp /etc/newrelic/newrelic.cfg.template /etc/newrelic/newrelic.cfg

# Disable OPcache
if [[ ${DISABLE_OPCACHE} -eq 1 ]]; then
    sed -i "s/opcache.validate_timestamps=0/opcache.validate_timestamps=1/" /etc/php.d/opcache.ini
fi

# Enable New Relic APM metrics if license key is set
if [[ -n ${NR_INSTALL_KEY} ]]; then
    newrelic-install install
    sed -i "s/newrelic.appname = \"PHP Application\"/newrelic.appname = \"${NR_APP_NAME}\"/" /etc/php.d/newrelic.ini
fi

# Change Supervisor UI username & password if specified
if [[ -n ${SUPERVISOR_USERNAME} ]]; then
    sed -i "s/username=admin/username=${SUPERVISOR_USERNAME}/" /etc/supervisord.conf
fi
if [[ -n ${SUPERVISOR_PASSWORD} ]]; then
    sed -i "s/password=admin/password=${SUPERVISOR_PASSWORD}/" /etc/supervisord.conf
fi

# Start Supervisord (PID 1) and it will start PHP-FPM and Nginx
exec /usr/bin/supervisord -c /etc/supervisord.conf
